﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;

namespace StationMapConfigTools
{
    public partial class frmStart : Form
    {
        public frmStart()
        {
            InitializeComponent();
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 2000;
            timer.Enabled = true;
            timer.Tick += new EventHandler(timer_Tick);
        }
        private Thread th = null;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            th = new Thread(new ThreadStart(Init));
            th.IsBackground = true;
            th.Start();
        }
        private delegate void CloseForm();
        private bool isRunning = true;
        private void Init()
        {
            try
            {
                /************************
                 * 
                 * ********************/

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Assert(false, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            CloseForm cf = delegate()
            {
                mut.WaitOne();
                isRunning = false;
                mut.ReleaseMutex();
            };
            cf.Invoke();
        }
        private Regex regex = null;
        private void FindFile( string StrDirName )
        {
            if (!Directory.Exists(StrDirName)) return;
            DirectoryInfo DirInfo = new DirectoryInfo(StrDirName);
            foreach (FileInfo _fi in DirInfo.GetFiles())
            {
                if (regex.IsMatch(_fi.Name))
                {
                    System.Diagnostics.Debug.WriteLine(_fi.FullName);
                }
            }
            foreach (DirectoryInfo _dir in DirInfo.GetDirectories())
            {
                FindFile(_dir.Name);
            }
        }


        private System.Windows.Forms.Timer timer = null;
        private System.Threading.Mutex mut = new System.Threading.Mutex();
        void timer_Tick(object sender, EventArgs e)
        {
            mut.WaitOne();
            if (!isRunning)
            {
                timer.Enabled = false;
                mut.ReleaseMutex();
                base.Close();
            }
            else
            {
                mut.ReleaseMutex();
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            msg = new Message();
            if (keyData == Keys.Escape || keyData == Keys.Back)
            {
                Application.Exit();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
